package com.jaychen.tliaswebmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jaychen.tliaswebmanagement.mapper.ClazzMapper;
import com.jaychen.tliaswebmanagement.pojo.ClassQueryParam;
import com.jaychen.tliaswebmanagement.pojo.Clazz;
import com.jaychen.tliaswebmanagement.pojo.PageResult;
import com.jaychen.tliaswebmanagement.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Override
    public PageResult<Clazz> page(ClassQueryParam classQueryParam) {
        PageHelper.startPage(classQueryParam.getPage(), classQueryParam.getPageSize());
        Page<Clazz> clazzPage = clazzMapper.list(classQueryParam);
        return new PageResult<Clazz>(clazzPage.getTotal(), clazzPage.getResult());
    }

    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }

    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }

    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }

    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> getAll() {
        return clazzMapper.getAll();
    }
}
